#! /bin/sh

if test x"$1" = x"-h" -o x"$1" = x"--help" ; then

echo "Usage: ./configure [options]"
echo ""
echo "available options:"
echo ""
echo "  --help                   print this message"
echo "  --enable-avis-input      enables avisynth input (win32 only)"
echo "  --enable-mp4-output      enables mp4 output (using gpac)"
echo "  --enable-vfw             compiles the VfW frontend"
echo "  --enable-pthread         enables multithreaded encoding"
echo "  --enable-debug           adds -g, doesn't strip"
echo "  --enable-visualize       enables visualization (X11 only)"
echo "  --extra-cflags=ECFLAGS   add ECFLAGS to CFLAGS"
echo "  --extra-ldflags=ELDFLAGS add ELDFLAGS to LDFLAGS"
echo ""
exit 1
fi

prefix='/usr/local'
eprefix='${prefix}'
bindir='${eprefix}/bin'
libdir='${eprefix}/lib'
includedir='${prefix}/include'

avis_input="no"
mp4_output="no"
pthread="no"
debug="no"
vfw="no"
vis="no"

CC="gcc"
CFLAGS="-Wall -I. -O4 -ffast-math -D__X264__"
LDFLAGS=""
HAVE_GETOPT_LONG=1

AS="nasm"
ASFLAGS="-f elf"

EXE=""

UNAMES="`uname -s`"
case "$UNAMES" in
  BeOS)
    SYS="BEOS"
    CFLAGS="$CFLAGS -DHAVE_MALLOC_H"
    ;;
  Darwin)
    SYS="MACOSX"
    CFLAGS="$CFLAGS -falign-loops=16"
    LDFLAGS="$LDFLAGS -lm -lmx"
    ;;
  FreeBSD)
    SYS="FREEBSD"
    LDFLAGS="$LDFLAGS -lm"
    ;;
  NetBSD)
    SYS="NETBSD"
    LDFLAGS="$LDFLAGS -lm"
    ;;
  Linux)
    SYS="LINUX"
    CFLAGS="$CFLAGS -DHAVE_MALLOC_H"
    LDFLAGS="$LDFLAGS -lm"
    ;;
  CYGWIN*)
    SYS="CYGWIN"
    CFLAGS="$CFLAGS -mno-cygwin"
    LDFLAGS="$LDFLAGS -mno-cygwin"
    ASFLAGS="-f win32 -DPREFIX"
    EXE=".exe"
    ;;
  MINGW*)
    SYS="MINGW"
    ASFLAGS="-f win32 -DPREFIX"
    EXE=".exe"
    ;;
  SunOS)
    SYS="SunOS"
    CFLAGS="$CFLAGS -DHAVE_MALLOC_H"
    LDFLAGS="$LDFLAGS -lm"
    HAVE_GETOPT_LONG=0
    ;;
  *)
    echo "Unknown system $UNAMES, edit the configure"
    exit 1
    ;;
esac

UNAMEM="`uname -m`"
case "$UNAMEM" in
  i386|i486|i586|i686|BePC)
    ARCH="X86"
    CFLAGS="$CFLAGS -DHAVE_MMXEXT -DHAVE_SSE2"
    ;;
  x86_64)
    ARCH="X86_64"
    CFLAGS="$CFLAGS -DHAVE_MMXEXT -DHAVE_SSE2"
    AS="yasm"
    ASFLAGS="-f elf -m amd64"
    ;;
  "Power Macintosh"|ppc)
    ARCH="PPC"
    if [ $SYS = MACOSX ]
    then
      CFLAGS="$CFLAGS -faltivec"
    else
      CFLAGS="$CFLAGS -maltivec -mabi=altivec"
    fi
    ;;
  sun4m|sun4d)
    ARCH="Sparc"
    ;;
  sun4u)
    ARCH="UltraSparc"
    CFLAGS="$CFLAGS -mcpu=ultrasparc"
    LDFLAGS="$LDFLAGS -mcpu=ultrasparc"
    AS="as"
    ASFLAGS="-xarch=v8plusa"
    ;;
  *)
    echo "Unknown platform $UNAMEM, edit the configure"
    exit 1
    ;;
esac

CFLAGS="$CFLAGS -DARCH_$ARCH -DSYS_$SYS"

# parse options

for opt do
    optarg="${opt#*=}"
    case "$opt" in
	--prefix=*)
	    prefix="$optarg"
	    ;;
	--exec-prefix=*)
	    eprefix="$optarg"
	    ;;
	--bindir=*)
	    bindir="$optarg"
	    ;;
	--libdir=*)
	    libdir="$optarg"
	    ;;
	--includedir=*)
	    includedir="$optarg"
	    ;;
        --enable-avis-input)
            if [ $SYS = CYGWIN -o $SYS = MINGW ]; then
                CFLAGS="$CFLAGS -DAVIS_INPUT"
                LDFLAGS="$LDFLAGS -lvfw32"
                avis_input="yes"
            fi
            ;;
        --enable-mp4-output)
            CFLAGS="$CFLAGS -DMP4_OUTPUT"
            LDFLAGS="$LDFLAGS -lm4systems_static"
            if [ $SYS = CYGWIN -o $SYS = MINGW ]; then
                LDFLAGS="$LDFLAGS -lwinmm"
            fi
            mp4_output="yes"
            ;;
        --extra-cflags=*)
            CFLAGS="$CFLAGS ${opt#--extra-cflags=}"
            VFW_CFLAGS="${opt#--extra-cflags=}"
            ;;
        --extra-ldflags=*)
            LDFLAGS="$LDFLAGS ${opt#--extra-ldflags=}"
            VFW_LDFLAGS="${opt#--extra-ldflags=}"
            ;;
        --enable-pthread)
            CFLAGS="$CFLAGS -DHAVE_PTHREAD=1"
            pthread="yes"
            case $SYS in
              MINGW|CYGWIN|BEOS)
                ;;
              *)
                LDFLAGS="$LDFLAGS -lpthread"
                ;;
            esac
            ;;
        --enable-debug)
            CFLAGS="$CFLAGS -g"
            debug="yes"
            ;;
        --enable-vfw)
            vfw="yes"
            ;;
        --enable-visualize)
            LDFLAGS="$LDFLAGS -L/usr/X11R6/lib -lX11"
            CFLAGS="$CFLAGS -DVISUALIZE=1"
            vis="yes"
            ;;
        *)
            echo "Unknown option $opt, ignored"
            ;;
    esac
done

VFWFLAGS=
if [ "$debug" != "yes" ]; then
    CFLAGS="$CFLAGS -s -fomit-frame-pointer"
    LDFLAGS="$LDFLAGS -s"
    VFW_LDFLAGS="$VFW_LDFLAGS -s"
fi

rm -f config.mak
cat > config.mak << EOF
prefix=$prefix
eprefix=$eprefix
bindir=$bindir
libdir=$libdir
includedir=$includedir
ARCH=$ARCH
SYS=$SYS
CC=$CC
CFLAGS=$CFLAGS
LDFLAGS=$LDFLAGS
AS=$AS
ASFLAGS=$ASFLAGS
VFW=$vfw
EXE=$EXE
VIS=$vis
HAVE_GETOPT_LONG=$HAVE_GETOPT_LONG
EOF

if [ "$vfw" = "yes" ]; then
    rm -f vfw/build/cygwin/config.mak
    cat > vfw/build/cygwin/config.mak << EOF
CFLAGS=$VFW_CFLAGS
LDFLAGS=$VFW_LDFLAGS
EOF
    echo "default: x264vfw.dll" >> config.mak
fi

echo "Platform:   $ARCH"
echo "System:     $SYS"
echo "avis input: $avis_input"
echo "mp4 output: $mp4_output"
echo "pthread:    $pthread"
echo "vfw:        $vfw"
echo "debug:      $debug"
echo "visualize:  $vis"
echo
echo "You can run 'make' now."

